<?php

namespace App\Admin\Controllers;

use App\Models\Student;
use App\Models\Squad;
use App\Models\Class_type;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Admin\Extensions\ExcelExpoter;
use Maatwebsite\Excel\Facades\Excel;

class StudentController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content){

            $content->header(trans('admin.student'));
            $content->description(trans('admin.list'));
            $content->body($this->grid()->render());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.student'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header(trans('admin.student'))
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.student'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Student::class, function (Grid $grid) {
            $grid->id(trans('admin.ID'))->sortable();
            $grid->no(trans('admin.student_no'))->sortable();
            $grid->ch_name(trans('admin.ch_name'));
            $grid->en_name(trans('admin.en_name'));
            $grid->sex(trans('admin.sex'))->display(function ($sex) {
                if($this->sex==1){
                    return trans('admin.men');
                }elseif ($this->sex==2) {
                    return trans('admin.women');
                }else{
                    return 'N/A';
                }
            })->sortable();
            $grid->squad_id(trans('admin.squad_id'))->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            });
            //$grid->squad_id(trans('admin.squad_id'))->editable('select', Squad::all()->pluck('name', 'id'));
            $grid->enrollment_time(trans('admin.enrollment_time'))->display(function (){
                    return date('Y/m/d',strtotime($this->enrollment_time));
            })->sortable();
            $grid->status(trans('admin.status'))->display(function ($status) {
                if($this->status==1){
                    return trans('admin.zaidu');
                }elseif ($this->status==2) {
                    return trans('admin.xiuxue');
                }elseif ($this->status==3) {
                    return trans('admin.tuixue');
                }else{
                    return 'N/A';
                }
            })->sortable();
            $grid->grade('年级')->display(function(){
                if(!is_null($this->grade) && $this->grade != ''){
                    $arr=['小班','中班','大班','一年级','二年级','三年级','四年级','五年级','六年级','七年级','八年级','九年级','高一','高二','高三'];
                    return $arr[$this->grade-1];
                }else{
                    return '';
                }
            });
            $grid->school('学校');
            //$grid->status(trans('admin.status'))->editable('select', [1 => trans('admin.zaidu'), 2 => trans('admin.xiuxue'), 3 => trans('admin.tuixue')])->sortable();

            //禁用导出按钮
            //$grid->disableExport();
            $filename="所有学生列表";
            $grid->exporter(new ExcelExpoter($grid,$filename));

            // $grid->tools(function ($tools) { 
            //     //关闭批量删除 
            //     $tools->batch(function ($batch) { 
            //         $batch->disableDelete(); 
            //     });
            // });
            // $grid->tools(function ($tools) {
            //     $tools->batch(function ($batch) {
            //         $batch->add('转班', new ChangeSquad());
            //     });
            // });

            $grid->actions(function ($actions) {
                $actions->disableView();
                $actions->disableEdit();
            });

            //筛选弹出框字段
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();
                    $filter->like('no',trans('admin.student_no'));
                    $filter->like('en_name',trans('admin.en_name'));
                    $filter->between('enrollment_time', trans('admin.enrollment_time'))->datetime(); 
                    $filter->equal('squad_id',trans('admin.squad_id'))->select(Squad::all()->pluck('name', 'id'));
                });
                $filter->column(1/2, function ($filter) {
                    $filter->like('ch_name',trans('admin.ch_name'));
                    $filter->equal('sex', trans('admin.sex'))->select($options = ['1'=> trans('admin.men') , '2'=> trans('admin.women')]);
                    $filter->equal('status', trans('admin.status'))->select($options = ['1'=> trans('admin.zaidu') , '2'=> trans('admin.xiuxue'), '3'=> trans('admin.tuixue')]);
                });
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Student::class, function (Form $form) {
            if (date('m')<7) {
                $a = '1';//当前月份小于7，则是春季学期，学号中标识为1
            }elseif (date('m')>7) {
                $a = '2';
            }
            if (Student::where('no','LIKE',substr(date('Y'), -2).$a.'%')->count()==0) {
                $no = substr(date('y'), -2).$a.'00001';
            }else{
                $max_no = Student::max('no');
                //$max_no = Student::where('id',$max_id)->value('no');
                $no =  $max_no+1;
            }
            
            $form->display('id', 'ID');
            //$form->text('no', trans('admin.student_no'))->rules('required')->addElementLabelClass('must-input');
            $form->text('no',trans('admin.student_no'))->value($no)->attribute('readonly','readonly');
            $form->text('ch_name', trans('admin.ch_name'))->rules('required')->addElementLabelClass('must-input');
            $form->text('en_name', trans('admin.en_name'))->rules('required')->addElementLabelClass('must-input');
            $form->radio('sex', trans('admin.sex'))->options(['1'=> trans('admin.men'),'2'=> trans('admin.women')])->rules('required')->addElementLabelClass('must-input');
            $form->select('squad_id', trans('admin.squad_id'))->options(Squad::all()->pluck('name', 'id'))->rules('required')->addElementLabelClass('must-input');
            $form->date('enrollment_time', trans('admin.enrollment_time'))->format('YYYY-MM-DD')->rules('required')->addElementLabelClass('must-input');
            $form->radio('status', trans('admin.status'))->options(['1' => trans('admin.zaidu'), '2'=> trans('admin.xiuxue'),'3'=> trans('admin.tuixue')])->default('1');
            //去掉添加/编辑表单的重置按钮
            $form->disableReset();
        });
    }

    public function quit_student()
    {
        return Admin::content(function (Content $content){

            $content->header('休学学生');
            $content->description(trans('admin.list'));
            $content->body($this->quit_grid()->render());
            $content->body($this->quit_view());
        });
    }

    protected function quit_grid()
    {
        return Admin::grid(Student::class, function (Grid $grid) {
            $grid->model()->where('status','=','2');
            $grid->no(trans('admin.student_no'))->sortable();
            $grid->ch_name(trans('admin.ch_name'));
            $grid->en_name(trans('admin.en_name'));
            $grid->sex(trans('admin.sex'))->display(function ($sex) {
                if($this->sex==1){
                    return trans('admin.men');
                }elseif ($this->sex==2) {
                    return trans('admin.women');
                }else{
                    return 'N/A';
                }
            })->sortable();
            $grid->squad_id('休学前班级')->display(function($squad_id){
                return Squad::find($this->squad_id)->name;
            });
            //$grid->squad_id(trans('admin.squad_id'))->editable('select', Squad::all()->pluck('name', 'id'));
            $grid->enrollment_time(trans('admin.enrollment_time'))->display(function (){
                return date('Y/m/d',strtotime($this->enrollment_time));
            })->sortable();
            $grid->status(trans('admin.status'))->display(function ($status) {
                if($this->status==1){
                    return trans('admin.zaidu');
                }elseif ($this->status==2) {
                    return trans('admin.xiuxue');
                }elseif ($this->status==3) {
                    return trans('admin.tuixue');
                }else{
                    return 'N/A';
                }
            })->sortable();
            //$grid->status(trans('admin.status'))->editable('select', [1 => trans('admin.zaidu'), 2 => trans('admin.xiuxue'), 3 => trans('admin.tuixue')])->sortable();

            //禁用导出按钮
            $grid->disableExport();


             $grid->tools(function ($tools) {
                 //关闭批量删除
                 $tools->batch(function ($batch) {
                     $batch->disableDelete();
                 });
             });
            // $grid->tools(function ($tools) {
            //     $tools->batch(function ($batch) {
            //         $batch->add('转班', new ChangeSquad());
            //     });
            // });

            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
                $actions->append('<button class="btn btn-primary" onclick="back('.$actions->getKey().')">复学</button>');
            });

            //筛选弹出框字段
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    // 去掉默认的id过滤器
                    $filter->disableIdFilter();
                    $filter->like('no',trans('admin.student_no'));
                    $filter->like('en_name',trans('admin.en_name'));
                    $filter->between('enrollment_time', trans('admin.enrollment_time'))->datetime();
                });
                $filter->column(1/2, function ($filter) {
                    $filter->like('ch_name',trans('admin.ch_name'));
                    $filter->equal('sex', trans('admin.sex'))->select($options = ['1'=> trans('admin.men') , '2'=> trans('admin.women')]);
                    $filter->equal('squad_id',trans('admin.squad_id'))->select(Squad::all()->pluck('name', 'id'));
                });
            });
        });
    }

    protected function quit_view()
    {
        return view('admin.quit_student');
    }
}
